Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(uniq): Improve performance of uniq function #40

Merged
merged 4 commits into from
Jun 13, 2024

Conversation

ho991217
Copy link
Contributor

What have been changed

Previous code used for ... of ... statement to check whether an item included in result array. I've changed it to return unique values with Set.

Improvement

  1. Time Complexity
    • for ... of ... is O(N). Array.prototype.includes does linear search, so it is O(N) considering the worst case scenario. So the code before had time complexity of O(N^2).
    • new Set()is O(N), Array.from() is also O(N), so total time complexity becomes O(N).
  2. Actual Calculation Time
    • I've tested both functions in this condition:
      • total item count: 100,000
      • duplicate item count: 25,000
      • unique item count: 75,000
    • and the results were:
      • Execution time of existing code: 542.303459 ms
      • Execution time of updated code: 3.931583 ms
      • screanshot 2024-06-13 16 12 20

Possible drawbacks

  1. Compatibility
    • Array.prototype.includes have out with ES7, on the other hand Set have done with ES6. So updated method also supports older version without polyfill.
  2. Keeping original order
    • Set also keeps its insertion order.
  3. Memory usage
    • uniq function creates Set object every time when function is called. There is chance that slight increase in memory usage. But I guess it does not make huge difference from making result array every time.

Added test cases

I've added test cases related to uniq function updates which assures its original functions.

Considering performance is primary factor of es-toolkit lib, this pr should be fair enough.
Let me know if other drawback does this pr have.

Copy link

vercel bot commented Jun 13, 2024

@ho991217 is attempting to deploy a commit to the Toss Team on Vercel.

A member of the Team first needs to authorize it.

@codecov-commenter
Copy link

codecov-commenter commented Jun 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (e7a04cd) to head (86bf1b8).
Report is 3 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##              main       #40   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           46        46           
  Lines          217       211    -6     
  Branches        20        19    -1     
=========================================
- Hits           217       211    -6     

src/array/uniq.ts Outdated Show resolved Hide resolved
@raon0211 raon0211 changed the title fix(uniq): Improve performance of uniq function perf(uniq): Improve performance of uniq function Jun 13, 2024
Copy link
Collaborator

@raon0211 raon0211 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It was originally implemented by using Set, but if the array is small, using arrays were more performant.

However I guess this will not make a big difference with small arrays, and will make a huge difference with big arrays. Thanks for your contribution again!

@raon0211 raon0211 merged commit d7c37c9 into toss:main Jun 13, 2024
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants